package com.harvest.scientific.ladder.dialog

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.AdapterView.OnItemClickListener
import android.widget.FrameLayout
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.adpater.BaseQuickAdapter
import com.harvest.scientific.ladder.databinding.AmountSelectPopupBinding
import com.harvest.scientific.ladder.databinding.ContainRecyclerviewBinding
import com.harvest.scientific.ladder.databinding.SingleRvBinding
import com.harvest.scientific.ladder.ext.dp2px
import com.harvest.scientific.ladder.ext.getScreenWidth
import com.harvest.scientific.ladder.ext.invisible
import com.harvest.scientific.ladder.ext.setMaterialDrawable
import com.harvest.scientific.ladder.ext.visible
import com.harvest.scientific.ladder.ext.visible2
import com.harvest.scientific.ladder.pop.basepopup.BasePopupWindow

/**
 * @author：zenglingwen
 * @date：2024-06-15 19:02
 * @desc：
 */
class AmountSelectedPopup(context: Context, val datas: List<String>, onItemClickListener: BaseQuickAdapter.OnItemClickListener<String>): BasePopupWindow(context) {

    private class AmountItemViewHolder(val binding: AmountSelectPopupBinding) : RecyclerView.ViewHolder(binding.root)

    init {

        val binding = ContainRecyclerviewBinding.inflate(LayoutInflater.from(context))
        width = getScreenWidth() - dp2px(30f).toInt() * 2
        contentView = binding.root
        val shapeAppearanceModel = ShapeAppearanceModel.builder().setBottomLeftCornerSize(dp2px(20f))
           .setBottomRightCornerSize(dp2px(20f))
            .build()
        binding.recyclerview.background = MaterialShapeDrawable(shapeAppearanceModel).apply {
            fillColor = ColorStateList.valueOf(Color.WHITE)
//            elevation = dp2px(10f) // 设置 elevation 以生成阴影
//            shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
//            paintStyle = Paint.Style.FILL
//            initializeElevationOverlay(context)
//            setShadowColor(Color.GRAY)
        }
        binding.recyclerview.updateLayoutParams<FrameLayout.LayoutParams> {
            leftMargin = dp2px(5f).toInt()
            rightMargin = dp2px(5f).toInt()
            bottomMargin = dp2px(5f).toInt()
        }
        binding.recyclerview.adapter = object : BaseQuickAdapter<String, AmountItemViewHolder> (datas) {
            override fun onCreateViewHolder(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): AmountItemViewHolder{
                val binding = AmountSelectPopupBinding.inflate(LayoutInflater.from(context))
                return AmountItemViewHolder(binding)
            }

            override fun onBindViewHolder(
                holder: AmountItemViewHolder,
                position: Int,
                item: String?
            ) {
                holder.binding.run {
                    amountTv.text = context.getString(R.string.xof, item)
                    dividerLine.visible2(holder.bindingAdapterPosition != datas.size - 1)
                }
            }

        }.apply {
            setOnItemClickListener(onItemClickListener)
        }

    }
}